package controller;

import controller.User;
import model.UserMapper;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

/***
 *@title HelloController
 *@description <TODO description class>
 *@author reps
 *@version 1.0.0
 *@create 2024/11/30 上午9:16
 **/
@RestController
public class HelloController {
    @GetMapping("/hello")
    public String hello() {
        try (InputStream inputStream = Resources.getResourceAsStream("mybatis-config.xml")) {
            SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
            try (SqlSession session = sqlSessionFactory.openSession()) {
                UserMapper mapper = session.getMapper(UserMapper.class);
                List<User> users = mapper.findAll();
                System.out.println(users.size());
                System.out.println(users);
                for(User user:users){
                    System.out.println(user.toString());
                }

                // System.out.println("User Name: " + user.getName());
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return "hello,Springboot!";
    }
}
